package com.lwb.ztx.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;

/**
 * Created by luweibin on 2016/5/30.
 */
public class NetWorkStateReceiver extends BroadcastReceiver {
    private OnNetworkStateChangeListener listener;

    public NetWorkStateReceiver() {
    }

    public NetWorkStateReceiver(OnNetworkStateChangeListener listener) {
        this.listener = listener;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mobileInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        NetworkInfo wifiInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (mobileInfo.isConnected() || wifiInfo.isConnected()) {
//           Toast.makeText(context,"网络已连接！",Toast.LENGTH_SHORT).show();
            if (listener != null) {
                listener.onNetWorkConnected();
            }
        }else {
//            Toast.makeText(context,"网络未连接！",Toast.LENGTH_SHORT).show();
            if (listener != null) {
                listener.onNetWorkDisConnected();
            }
        }
    }

    public interface OnNetworkStateChangeListener {
        void onNetWorkConnected();
        void onNetWorkDisConnected();
    }
}